<template>
  <div class="admin-container">
    <!-- 垂直悬浮Tab栏 -->
    <div class="vertical-tabs">
      <div class="tab-header">
        <div class="tab-logo">
          <Settings :size="24" />
        </div>
        <div class="tab-title">管理</div>
      </div>
      
      <div class="tab-list">
        <div 
          v-for="tab in tabs" 
          :key="tab.key"
          :class="['tab-item', { active: selectedKeys.includes(tab.key) }]"
          @click="handleTabClick(tab.key)"
        >
          <div class="tab-icon">
            <component :is="tab.icon" :size="20" />
          </div>
          <div class="tab-label">{{ tab.label }}</div>
        </div>
      </div>
    </div>
    
    <!-- 主内容区域 -->
    <div class="main-content">
      <div class="content-wrapper">
        <router-view />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref,  onMounted, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { Settings, Volume2, Database, Users, FileText, BarChart3 } from 'lucide-vue-next'

const route = useRoute()
const router = useRouter()

// 当前选中的菜单项
const selectedKeys = ref<string[]>(['dashboard'])

// Tab配置
const tabs = [
  {
    key: 'dashboard',
    label: '仪表盘',
    icon: BarChart3
  },
  {
    key: 'voice',
    label: '音色',
    icon: Volume2
  },
  {
    key: 'knowledge',
    label: '知识库',
    icon: Database
  },
  {
    key: 'role',
    label: '角色',
    icon: Users
  },
  {
    key: 'logs',
    label: '日志',
    icon: FileText
  }
]

// 根据当前路由设置选中的菜单项
const updateSelectedKeys = () => {
  const path = route.path
  if (path.includes('/admin/dashboard')) {
    selectedKeys.value = ['dashboard']
  } else if (path.includes('/admin/voice')) {
    selectedKeys.value = ['voice']
  } else if (path.includes('/admin/knowledge')) {
    selectedKeys.value = ['knowledge']
  } else if (path.includes('/admin/role')) {
    selectedKeys.value = ['role']
  } else if (path.includes('/admin/logs')) {
    selectedKeys.value = ['logs']
  } else {
    selectedKeys.value = ['dashboard'] // 默认选中仪表盘
  }
}

// 处理Tab点击
const handleTabClick = (key: string) => {
  router.push(`/admin/${key}`)
}

// 组件挂载时更新选中状态
onMounted(() => {
  updateSelectedKeys()
  // 如果当前路径是 /admin，重定向到 /admin/dashboard
  if (route.path === '/admin') {
    router.replace('/admin/dashboard')
  }
})

// 监听路由变化
watch(() => route.path, () => {
  updateSelectedKeys()
})
</script>

<style scoped>
.admin-container {
  height: max(100vh, 1480px);
  /* background: #f8fafc; */
  display: flex;
  position: relative;
  max-width: max(1600px, 80%);
  margin: 0 auto;
  padding: 40px;
}

/* 垂直悬浮Tab栏 */
.vertical-tabs {
  position: fixed;
  left: 24px;
  top: 50%;
  transform: translateY(-50%);
  width: 80px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
  border: 1px solid rgba(255, 255, 255, 0.2);
  z-index: 1000;
  overflow: hidden;
}

.tab-header {
  padding: 20px 0;
  text-align: center;
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
}

.tab-logo {
  width: 40px;
  height: 40px;
  margin: 0 auto 8px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.tab-title {
  font-size: 12px;
  font-weight: 600;
  color: #64748b;
  letter-spacing: 0.5px;
}

.tab-list {
  padding: 16px 0;
}

.tab-item {
  position: relative;
  margin: 8px 12px;
  padding: 8px 0;
  border-radius: 16px;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  text-align: center;
  background: transparent;
}

.tab-item:hover {
  background: rgba(102, 126, 234, 0.08);
  transform: translateY(-2px);
}

.tab-item.active {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
  transform: translateY(-2px);
}

.tab-item.active::before {
  content: '';
  position: absolute;
  left: -12px;
  top: 50%;
  transform: translateY(-50%);
  width: 4px;
  height: 24px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 2px;
}

.tab-icon {
  margin-bottom: 6px;
  color: #64748b;
  transition: color 0.3s ease;
}

.tab-item:hover .tab-icon {
  color: #667eea;
}

.tab-item.active .tab-icon {
  color: white;
}

.tab-label {
  font-size: 11px;
  font-weight: 500;
  color: #64748b;
  transition: color 0.3s ease;
  line-height: 1.2;
}

.tab-item:hover .tab-label {
  color: #667eea;
}

.tab-item.active .tab-label {
  color: white;
}

/* 主内容区域 */
.main-content {
  flex: 1;
  margin-left: 120px;
  padding: 24px;
  min-height: max(100vh, 1000px);
}

.content-wrapper {
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.06);
  /* min-height: calc(100vh - 48px); */
  padding: 32px;
  border: 1px solid rgba(0, 0, 0, 0.04);
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .vertical-tabs {
    width: 70px;
    left: 16px;
  }
  
  .main-content {
    margin-left: 100px;
    padding: 16px;
  }
  
  .tab-item {
    padding: 12px 0;
  }
  
  .tab-label {
    font-size: 10px;
  }
}

@media (max-width: 768px) {
  .vertical-tabs {
    position: fixed;
    bottom: 24px;
    left: 50%;
    top: auto;
    transform: translateX(-50%);
    width: auto;
    height: 70px;
    flex-direction: row;
    padding: 0 20px;
  }
  
  .tab-header {
    display: none;
  }
  
  .tab-list {
    display: flex;
    align-items: center;
    padding: 0;
    gap: 8px;
  }
  
  .tab-item {
    margin: 0;
    padding: 12px;
    min-width: 60px;
  }
  
  .main-content {
    margin-left: 0;
    padding: 16px;
    padding-bottom: 120px;
  }
}
</style>